package com;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.jgrapht.graph.DefaultWeightedEdge;
import org.jgrapht.graph.SimpleWeightedGraph;
import org.xml.sax.SAXException;


public class ParseAndCreateGraph {
	
	/*This method uses the SAX parser to parse the XML file of Cambridge using my handler.*/
	
	public static SimpleWeightedGraph<Node, DefaultWeightedEdge> getGraph() throws ParserConfigurationException {
	
		//create the factory
		SAXParserFactory factory = SAXParserFactory.newInstance();	
		
	try {
				
		//create a parser
		SAXParser parser = factory.newSAXParser();
		
		//find the XML inside the applet jar and return its URL. 
		URL uRL = ParseAndCreateGraph.class.getResource("/map.xml");
		
		//open a connection to the URL and get a stream for the parser to read.
		URLConnection connection = uRL.openConnection();
		InputStream inStream = connection.getInputStream();
		
		//parse the XML file using my handler
		parser.parse(inStream, new OSMHandler());
		
        
    } catch (SAXException e) {
    	System.out.println(e);
    } catch (IOException e) {
    	System.out.println(e);
    } catch (ParserConfigurationException e) {
    	System.out.println(e);
    }
	
    return OSMHandler.graph;
    
	}
	
}
 
